/*
 * 作者：姚俊
 * 功能：项目报表
 * 日期：2015-05-24
 */
package admin.databaseToExcel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

import admin.action.fruits.Constant;
import admin.service.fruits.ProjectService;

import teacher.bean.Project;
import teacher.service.project;
import tools.StringDate;

public class PullPorjectToExcel {
	
	/**
	 * @category 生成项目的报表
	 * @author yaojun
	 * @param flag int 值为0查询全部、1按照起止日期查询、2按照获奖名称模糊查询
	 * @param project1 Project 传入一个项目的对象，当查询全部的时候该对象为空，当按照起止日期查询的时候，该对象必须有日期
	 * 当进行名称模糊查询的时候该对象中必须有名称
	 */
	public static void projectExcel(int flag,Project project,int direction)
	{		
        //项目工程列表
        List<Project> projects=null;
        
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"project.xls";
//            String fileName="D://projects.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0);  
               
               
               if(flag==0)
               {
            	   projects=new project().selectAllProject();
               }
               else if(flag==1)
               {
            	   projects=new project().selectTimeProject(project.getBeginDate(), project.getFinishDate());
               }
               else if(flag==2)
               {
            	   projects=new project().selectVagueProject(project.getName());
               }
               else
               {
            	   projects=new ProjectService().getProjectByAll(project,direction, Constant.state);
               }
               
               
               
               //要插入到的Excel表格的行号，默认从0开始
               Label pid= new Label(0, 0, "编号");//表示第
               Label pname= new Label(1, 0, "项目名称");//表示第
               Label pclass_= new Label(2, 0, "项目种类");
               Label psource= new Label(3, 0, "项目编号");   
               Label pfunds= new Label(4, 0, "经费（横向为合同金额）（万元）");
               Label pleader= new Label(5, 0, "负责人");
//               Label pcommunicate= new Label(7, 0, "通讯人员");
               Label pbeginDate= new Label(6, 0, "项目开始日期");
               Label pfinishDate= new Label(7, 0, "项目截止日期");
               
               
               ws.addCell(pid);
               ws.addCell(pname);
               ws.addCell(pclass_);
               ws.addCell(psource);
               ws.addCell(pfunds);
               ws.addCell(pleader);
//               ws.addCell(pcommunicate);
               ws.addCell(pbeginDate);
               ws.addCell(pfinishDate);
               

               for (int i = 0; i < projects.size(); i++) {  
            	   String kind=null;
            	   Label ppid= new Label(0, i+1, i+1+"");
                   Label ppname= new Label(1, i+1, projects.get(i).getName()+"");
                   if(projects.get(i).getKind()==0){
                	   kind="纵向";
                   }else {
                	   kind="横向";
                   }
                   Label ppclass_= new Label(2, i+1, kind+"");
                   Label ppsource= new Label(3, i+1, projects.get(i).getNumber()+"");  
                   Label ppfunds= new Label(4, i+1, projects.get(i).getFunds()+"");  
                   Label ppleader= new Label(5, i+1, projects.get(i).getLeader()+"");  
//                   Label ppcommunicate= new Label(8, i+1, projects.get(i).getCommunicate()+"");
                   Label ppbeginDate= new Label(6, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   projects.get(i).getBeginDate())))); 
                   Label ppfinishDate= new Label(7, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   projects.get(i).getFinishDate())))); 
                   
                   
                   ws.addCell(ppid);
                   ws.addCell(ppname);
                   ws.addCell(ppclass_);
                   ws.addCell(ppsource);          
                   ws.addCell(ppfunds);
                   ws.addCell(ppleader);
//                   ws.addCell(ppcommunicate);          
                   ws.addCell(ppbeginDate);
                   ws.addCell(ppfinishDate);
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }


}
